﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;

    public class InOutInvDetailRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        private string currFlagAJ = string.Empty;
        private string currFlagOwner = string.Empty;
        private string currFlagReceipt = string.Empty;
        private DataSet dsQuery = null;
        protected HtmlForm F15336;
        protected GridView gvInOutInvDetailRpt;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        private decimal lastEndInvQty = 0M;
        private string lastFlagAJ = string.Empty;
        private string lastFlagOwner = string.Empty;
        private string lastFlagReceipt = string.Empty;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        private decimal TotalEndQty;
        private decimal TotalQty;
        protected UpdatePanel up1;

        public void BindData2gvInOutStockDetailList(InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity)
        {
            inOutStockRptDetailQueryEntity.IsGetAll = true;
            this.dsQuery = AnalyticsReportServiceClientProxy.GetInstance().GetInOutInvRptDetailDataSet(inOutStockRptDetailQueryEntity);
            base.BindData2GridView(this.gvInOutInvDetailRpt, this.dsQuery);
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            inOutStockRptDetailQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = inOutStockRptDetailQueryEntity.CurrentPage;
            inOutStockRptDetailQueryEntity.CurrentPage = "0";
            if ((this.dsQuery != null) && (this.dsQuery.Tables[0].Rows.Count > 0))
            {
                base.Export2Excel("InOutInvDetailRpt.xls", "15336", this.gvInOutInvDetailRpt, this.SetExportData(this.dsQuery, inOutStockRptDetailQueryEntity));
            }
            else
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetInOutInvRptDetailDataSet(inOutStockRptDetailQueryEntity))
                {
                    if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                    {
                        base.Export2Excel("InOutInvDetailRpt.xls", "15336", this.gvInOutInvDetailRpt, this.SetExportData(ds, inOutStockRptDetailQueryEntity));
                    }
                }
            }
            inOutStockRptDetailQueryEntity.IsGetAll = false;
            inOutStockRptDetailQueryEntity.CurrentPage = currentPageIndex;
            this.SetInOutStockDetailRptQueryEntity(inOutStockRptDetailQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            inOutStockRptDetailQueryEntity.OWNER_ID = "";
            inOutStockRptDetailQueryEntity.SKU_ID = "";
            inOutStockRptDetailQueryEntity.STARTDATETIME = "";
            inOutStockRptDetailQueryEntity.ENDDATETIME = "";
            inOutStockRptDetailQueryEntity.RECEIPT_ID = "";
            inOutStockRptDetailQueryEntity.VENDOR_ID = "";
            inOutStockRptDetailQueryEntity.CONSIGNEE_ID = "";
            inOutStockRptDetailQueryEntity.SKU_DESCR = "";
            inOutStockRptDetailQueryEntity.ORDER_ID = "";
            this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = this.GetInOutStockRptQueryEntity();
            this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
        }

        private InOutStockRptDetailQueryEntity GetInOutStockRptQueryEntity()
        {
            return (base.PageQueryEntitySession as InOutStockRptDetailQueryEntity);
        }

        protected void gvInOutInvDetailRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15336", this.gvInOutInvDetailRpt);
        }

        protected void gvInOutInvDetailRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                this.currFlagOwner = ((Label) e.Row.FindControl("lblOwnerID")).Text + ((Label) e.Row.FindControl("lblVendorID")).Text + ((Label) e.Row.FindControl("lblSkuID")).Text;
                this.currFlagReceipt = ((Label) e.Row.FindControl("lblReceiptID")).Text + ((Label) e.Row.FindControl("lblReceiptDate")).Text + ((Label) e.Row.FindControl("lblInQty")).Text + ((Label) e.Row.FindControl("lblExternalLot")).Text;
                if (!string.IsNullOrEmpty(this.currFlagOwner))
                {
                    if (this.currFlagOwner.ToUpper() == this.lastFlagOwner.ToUpper())
                    {
                        ((Label) e.Row.FindControl("lblOwnerID")).Text = "";
                        ((Label) e.Row.FindControl("lblVendorID")).Text = "";
                        ((Label) e.Row.FindControl("lblSkuID")).Text = "";
                        ((Label) e.Row.FindControl("lblSkuDescr")).Text = "";
                        ((Label) e.Row.FindControl("lblBeginQty")).Text = "";
                        decimal endInvQty = 0M;
                        if (this.currFlagReceipt.ToUpper() == this.lastFlagReceipt.ToUpper())
                        {
                            ((Label) e.Row.FindControl("lblReceiptID")).Text = "";
                            ((Label) e.Row.FindControl("lblReceiptDate")).Text = "";
                            ((Label) e.Row.FindControl("lblInQty")).Text = "";
                            ((Label) e.Row.FindControl("lblExternalLot")).Text = "";
                            ((Label) e.Row.FindControl("lblAJQty")).Text = "";
                            endInvQty = this.lastEndInvQty - decimal.Parse(((Label) e.Row.FindControl("lblOtQty")).Text);
                            ((Label) e.Row.FindControl("lblEndQty")).Text = endInvQty.ToString();
                        }
                        else
                        {
                            if (DateTime.Parse(((Label) e.Row.FindControl("lblReceiptDate")).Text) < DateTime.Parse(this.PreviousPage.StartTime))
                            {
                                ((Label) e.Row.FindControl("lblInQty")).Text = "0";
                            }
                            endInvQty = ((this.lastEndInvQty + decimal.Parse(((Label) e.Row.FindControl("lblInQty")).Text)) - decimal.Parse(((Label) e.Row.FindControl("lblOtQty")).Text)) + decimal.Parse(((Label) e.Row.FindControl("lblAJQty")).Text);
                            ((Label) e.Row.FindControl("lblEndQty")).Text = endInvQty.ToString();
                        }
                    }
                    else
                    {
                        if (DateTime.Parse(((Label) e.Row.FindControl("lblReceiptDate")).Text) < DateTime.Parse(this.PreviousPage.StartTime))
                        {
                            ((Label) e.Row.FindControl("lblInQty")).Text = "0";
                        }
                        ((Label) e.Row.FindControl("lblEndQty")).Text = (((decimal.Parse(((Label) e.Row.FindControl("lblBeginQty")).Text) + decimal.Parse(((Label) e.Row.FindControl("lblInQty")).Text)) - decimal.Parse(((Label) e.Row.FindControl("lblOtQty")).Text)) + decimal.Parse(((Label) e.Row.FindControl("lblAJQty")).Text)).ToString();
                    }
                    this.lastFlagReceipt = this.currFlagReceipt;
                    this.lastFlagOwner = this.currFlagOwner;
                    this.lastEndInvQty = decimal.Parse(((Label) e.Row.FindControl("lblEndQty")).Text);
                }
            }
            InOutStockRptDetailQueryEntity inOutStockRptQuery = this.GetInOutStockRptQueryEntity();
        }

        protected void gvInOutInvDetailRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
        }

        private void LoadData()
        {
            InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity = new InOutStockRptDetailQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    inOutStockRptDetailQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    inOutStockRptDetailQueryEntity.SKU_ID = this.PreviousPage.SkuID;
                    inOutStockRptDetailQueryEntity.STARTDATETIME = this.PreviousPage.StartTime;
                    inOutStockRptDetailQueryEntity.ENDDATETIME = this.PreviousPage.EndTime;
                    inOutStockRptDetailQueryEntity.RECEIPT_ID = this.PreviousPage.ReceiptID;
                    inOutStockRptDetailQueryEntity.VENDOR_ID = this.PreviousPage.VendorID;
                    inOutStockRptDetailQueryEntity.CONSIGNEE_ID = this.PreviousPage.ConsigneeID;
                    inOutStockRptDetailQueryEntity.SKU_DESCR = this.PreviousPage.Descr;
                    inOutStockRptDetailQueryEntity.ORDER_ID = this.PreviousPage.OrderID;
                }
                else
                {
                    inOutStockRptDetailQueryEntity.OWNER_ID = "";
                    inOutStockRptDetailQueryEntity.SKU_ID = "";
                    inOutStockRptDetailQueryEntity.STARTDATETIME = "";
                    inOutStockRptDetailQueryEntity.ENDDATETIME = "";
                    inOutStockRptDetailQueryEntity.RECEIPT_ID = "";
                    inOutStockRptDetailQueryEntity.VENDOR_ID = "";
                    inOutStockRptDetailQueryEntity.CONSIGNEE_ID = "";
                    inOutStockRptDetailQueryEntity.SKU_DESCR = "";
                    inOutStockRptDetailQueryEntity.ORDER_ID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            inOutStockRptDetailQueryEntity.IsGetAll = false;
            inOutStockRptDetailQueryEntity.CurrentPage = "0";
            inOutStockRptDetailQueryEntity.PageSize = this.gvInOutInvDetailRpt.PageSize.ToString();
            this.SetInOutStockDetailRptQueryEntity(inOutStockRptDetailQueryEntity);
            this.BindData2gvInOutStockDetailList(inOutStockRptDetailQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvInOutInvDetailRpt);
                this.LoadData();
            }
        }

        private DataSet SetExportData(DataSet ds, InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity)
        {
            string currFlagOwner = string.Empty;
            string lastFlagOwner = string.Empty;
            string currFlagReceipt = string.Empty;
            string lastFlagReceipt = string.Empty;
            decimal lastEndInvQty = 0M;
            try
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    decimal endInvQty;
                    currFlagOwner = ds.Tables[0].Rows[i]["OWNER_ID"].ToString() + ds.Tables[0].Rows[i]["VENDOR_ID"].ToString() + ds.Tables[0].Rows[i]["SKU_ID"].ToString();
                    currFlagReceipt = ds.Tables[0].Rows[i]["LOT_ATTR10"].ToString() + ds.Tables[0].Rows[i]["RECEIPT_DATE"].ToString() + ds.Tables[0].Rows[i]["INQTY"].ToString() + ds.Tables[0].Rows[i]["EXTERNAL_LOT"].ToString();
                    if (currFlagOwner.ToUpper() == lastFlagOwner.ToUpper())
                    {
                        ds.Tables[0].Rows[i]["OWNER_ID"] = "";
                        ds.Tables[0].Rows[i]["VENDOR_ID"] = "";
                        ds.Tables[0].Rows[i]["SKU_ID"] = "";
                        ds.Tables[0].Rows[i]["DESCR"] = "";
                        ds.Tables[0].Rows[i]["BEGINQTY"] = 0;
                        endInvQty = 0M;
                        if (currFlagReceipt.ToUpper() == lastFlagReceipt.ToUpper())
                        {
                            ds.Tables[0].Rows[i]["LOT_ATTR10"] = "";
                            ds.Tables[0].Rows[i]["RECEIPT_DATE"] = "";
                            ds.Tables[0].Rows[i]["INQTY"] = 0;
                            ds.Tables[0].Rows[i]["EXTERNAL_LOT"] = "";
                            ds.Tables[0].Rows[i]["AJQTY"] = 0;
                            endInvQty = lastEndInvQty - decimal.Parse(ds.Tables[0].Rows[i]["OTQTY"].ToString());
                            ds.Tables[0].Rows[i]["ENDQTY"] = endInvQty;
                        }
                        else
                        {
                            if (DateTime.Parse(ds.Tables[0].Rows[i]["RECEIPT_DATE"].ToString()) < DateTime.Parse(inOutStockRptDetailQueryEntity.STARTDATETIME))
                            {
                                ds.Tables[0].Rows[i]["INQTY"] = 0;
                            }
                            endInvQty = ((lastEndInvQty + decimal.Parse(ds.Tables[0].Rows[i]["INQTY"].ToString())) - decimal.Parse(ds.Tables[0].Rows[i]["OTQTY"].ToString())) + decimal.Parse(ds.Tables[0].Rows[i]["AJQTY"].ToString());
                            ds.Tables[0].Rows[i]["ENDQTY"] = endInvQty;
                        }
                    }
                    else
                    {
                        if (DateTime.Parse(ds.Tables[0].Rows[i]["RECEIPT_DATE"].ToString()) < DateTime.Parse(inOutStockRptDetailQueryEntity.STARTDATETIME))
                        {
                            ds.Tables[0].Rows[i]["INQTY"] = 0;
                        }
                        endInvQty = ((decimal.Parse(ds.Tables[0].Rows[i]["BEGINQTY"].ToString()) + decimal.Parse(ds.Tables[0].Rows[i]["INQTY"].ToString())) - decimal.Parse(ds.Tables[0].Rows[i]["OTQTY"].ToString())) + decimal.Parse(ds.Tables[0].Rows[i]["AJQTY"].ToString());
                        ds.Tables[0].Rows[i]["ENDQTY"] = endInvQty;
                    }
                    lastFlagReceipt = currFlagReceipt;
                    lastFlagOwner = currFlagOwner;
                    lastEndInvQty = decimal.Parse(ds.Tables[0].Rows[i]["ENDQTY"].ToString());
                }
            }
            catch
            {
                return null;
            }
            return ds;
        }

        public void SetInOutStockDetailRptQueryEntity(InOutStockRptDetailQueryEntity inOutStockRptDetailQueryEntity)
        {
            base.PageQueryEntitySession = inOutStockRptDetailQueryEntity;
        }

        public InOutInvDetailRptQuery PreviousPage
        {
            get
            {
                return (InOutInvDetailRptQuery) base.PreviousPage;
            }
        }
    }
}

